<?php

namespace frontend\modules\cart\models;

use Yii;

/**
 * This is the model class for table "cart".
 *
 * @property string $rec_id
 * @property string $user_id
 * @property string $session_id
 * @property string $goods_id
 * @property string $goods_sn
 * @property string $product_id
 * @property string $goods_name
 * @property string $market_price
 * @property string $goods_price
 * @property integer $goods_number
 * @property string $goods_attr
 * @property integer $is_real
 * @property string $extension_code
 * @property string $parent_id
 * @property integer $rec_type
 * @property integer $is_gift
 * @property integer $is_shipping
 * @property integer $can_handsel
 * @property string $goods_attr_id
 * @property string $cart_price
 * @property string $shop_price
 * @property string $is_check
 * @property integer $is_promote
 * @property integer $refere_id
 * @property integer $attr_cat_id
 * @property integer $goods_image
 * @property string $re_ordersn
 * @property string $re_orderid
 * @property string $thumb_img
 */
class Cart extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'cart';
    }


    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
//            TimestampBehavior::className(),
            // 名为myBehavior4的行为，也是给出了MyBehavior类的配置数组
            'uuid' => [
                'class' => \common\behavior\uuidBehavior::className(),
                'owner' => $this
            ]
        ];
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            ['rec_id','default','value'=>$this->getUUID()],
            [['rec_id'], 'required'],
            [[ 'product_id', 'goods_number', 'is_real', 'parent_id', 'rec_type', 'is_gift', 'is_shipping', 'can_handsel', 'is_promote','is_check'], 'integer'],
            [['market_price', 'goods_price', 'cart_price', 'shop_price'], 'number'],
            [['goods_attr','goods_id','refere_id','goods_image'], 'string'],
            [['rec_id', 'user_id','attr_cat_id','refere_id'], 'string', 'max' => 40],
            [['session_id'], 'string', 'max' => 32],
            [['goods_sn'], 'string', 'max' => 60],
            [['goods_name'], 'string', 'max' => 120],
            [['extension_code'], 'string', 'max' => 30],
            [['goods_attr_id','thumb_img'], 'string', 'max' => 255],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'rec_id' => 'Rec ID',
            'user_id' => 'User ID',
            'session_id' => 'Session ID',
            'goods_id' => 'Goods ID',
            'goods_sn' => 'Goods Sn',
            'product_id' => 'Product ID',
            'goods_name' => 'Goods Name',
            'market_price' => 'Market Price',
            'goods_price' => 'Goods Price',
            'goods_number' => 'Goods Number',
            'goods_attr' => 'Goods Attr',
            'is_real' => 'Is Real',
            'extension_code' => 'Extension Code',
            'parent_id' => 'Parent ID',
            'rec_type' => 'Rec Type',
            'is_gift' => 'Is Gift',
            'is_shipping' => 'Is Shipping',
            'can_handsel' => 'Can Handsel',
            'goods_attr_id' => 'Goods Attr ID',
            'cart_price' => 'Cart Price',
            'shop_price' => 'Shop Price',
            'is_check' => 'Is Check',
            'is_promote' => 'Is Promote',
            'refere_id'=> 'refere_id',
            'attr_cat_id'=> 'attr_cat_id',
            'goods_image'=>'goods_image'
        ];
    }

    /**
     * @inheritdoc
     * @return \frontend\modules\cart\models\query\CartQuery the active query used by this AR class.
     */
    public static function find()
    {
        return new \frontend\modules\cart\models\query\CartQuery(get_called_class());
    }

    /**
     * 获取当前用户购物车信息
     *
     * @return $this
     */
    public static function getCartByuser()
    {
        $userid = Yii::$app->UserInfo->userid;
        return static::find()->filterWhere(['user_id'=>$userid]);
    }

    /**
     * 获取打钩购物车信息
     *
     * @return array|Cart[]
     */
    public static function getCartWChecked()
    {
        $userid = Yii::$app->UserInfo->userid;
        $cartARR = static::getCartByuser()->where(['user_id'=>$userid,'is_check'=>1])->asArray()->all();
        return $cartARR;
    }
}
